package edu.ynu.se.xiecheng.achitectureclass.common.handler;

import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(UnauthenticatedException.class)
    public ResponseEntity<?> handleUnauthenticatedException(UnauthenticatedException e) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", false);
        response.put("message", "请先登录");
        response.put("error", "未认证");
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
    }

    @ExceptionHandler(UnauthorizedException.class)
    public ResponseEntity<?> handleUnauthorizedException(UnauthorizedException e) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", false);
        response.put("message", "没有权限");
        response.put("error", "未授权");
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body(response);
    }
} 